﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Hartley.ApplicationLogic;
using Hartley.DTOs;
using Hartley.Util;

namespace WebApplication1
{
    public partial class viewPrefectsBatches : System.Web.UI.Page
    {
        StudentAL studentAL;
        SportsUtil sportsUtil;
        private const int PREFECT_BATCH_PER_ROW = 1;
        private const int PREFECT_BATCH_PAGE_SIZE = 2;
        List<PrefectBatchDTO> allPrefectBatchData;
        protected int currentPageNumber = 1;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();

            }
        }

        private void BindData()
        {

            studentAL = new StudentAL();
            sportsUtil = new SportsUtil();
            allPrefectBatchData = new List<PrefectBatchDTO>();

            List<PrefectBatchDTO> allPrefectBatchOfOnePage = new List<PrefectBatchDTO>();

            allPrefectBatchData = studentAL.GetAllPrefectBatches().OrderByDescending(a => a.PrefectTo).ToList();
            //allALevelDataYrs = allALevelData.Select(a => a.Year).Distinct().ToList();
            //List<string> allPrefectBatches = allPrefectBatchData.Select(a => a.PrefectBatchName).ToList();

            // get the total rows 
            double totalRows = (int)allPrefectBatchData.Count;


            lblAllPrefectBatchPage.Text = "Displaying all of the College's Prefect Batches - Page ";
            lblAllPrefectBatchOf.Text = "  of  ";


            lblAllPrefectBatchTotalPage.Text = CalculateTotalPages(totalRows).ToString();


            //int al_curr_pg_no = Convert.ToInt32(Session["al_curr_pg_no"]);
            // int ol_curr_pg_no = Convert.ToInt32(Session["ol_curr_pg_no"]);
            lblAllPrefectBatchCurrPage.Text = currentPageNumber.ToString();


            if (Convert.ToInt32(lblAllPrefectBatchTotalPage.Text) < 3)
            {
                if (Int32.Parse(lblAllPrefectBatchTotalPage.Text) <= 0)
                {
                    btnAllPrefectBatchFirst.Enabled = false;
                    btnAllPrefectBatchPrev.Enabled = false;
                    btnAllPrefectBatchNext.Enabled = false;
                    btnAllPrefectBatchLast.Enabled = false;


                }

                if (currentPageNumber == 1)
                {
                    btnAllPrefectBatchFirst.Enabled = false;
                    btnAllPrefectBatchPrev.Enabled = false;



                    if (Int32.Parse(lblAllPrefectBatchTotalPage.Text) == 1)
                    {
                        allPrefectBatchOfOnePage = allPrefectBatchData.GetRange(0, Convert.ToInt32(totalRows));

                        btnAllPrefectBatchNext.Enabled = false;
                        btnAllPrefectBatchLast.Enabled = false;

                    }
                    else if (Int32.Parse(lblAllPrefectBatchTotalPage.Text) == 2)
                    {
                        allPrefectBatchOfOnePage = allPrefectBatchData.GetRange(0, PREFECT_BATCH_PAGE_SIZE * PREFECT_BATCH_PER_ROW);

                        btnAllPrefectBatchNext.Enabled = true;
                        btnAllPrefectBatchLast.Enabled = true;

                    }
                }

                else
                {

                    btnAllPrefectBatchPrev.Enabled = true;
                    btnAllPrefectBatchFirst.Enabled = true;

                    if (currentPageNumber == Int32.Parse(lblAllPrefectBatchTotalPage.Text))
                    {
                        allPrefectBatchOfOnePage = allPrefectBatchData.GetRange((currentPageNumber - 1) * PREFECT_BATCH_PAGE_SIZE * PREFECT_BATCH_PER_ROW
                            , (Convert.ToInt32(totalRows) - (currentPageNumber - 1) * PREFECT_BATCH_PAGE_SIZE * PREFECT_BATCH_PER_ROW));

                        btnAllPrefectBatchNext.Enabled = false;
                        btnAllPrefectBatchLast.Enabled = false;

                    }
                    else
                    {
                        allPrefectBatchOfOnePage = allPrefectBatchData.GetRange((currentPageNumber - 1) * PREFECT_BATCH_PAGE_SIZE * PREFECT_BATCH_PER_ROW, PREFECT_BATCH_PAGE_SIZE * PREFECT_BATCH_PER_ROW);

                        btnAllPrefectBatchNext.Enabled = true;
                        btnAllPrefectBatchLast.Enabled = true;


                    }

                }
            }


            else
            {
                if (currentPageNumber == 1)
                {
                    allPrefectBatchOfOnePage = allPrefectBatchData.GetRange(0, PREFECT_BATCH_PAGE_SIZE * PREFECT_BATCH_PER_ROW);

                    btnAllPrefectBatchFirst.Enabled = false;
                    btnAllPrefectBatchPrev.Enabled = false;
                    btnAllPrefectBatchNext.Enabled = true;
                    btnAllPrefectBatchLast.Enabled = true;

                }



                else
                {
                    btnAllPrefectBatchPrev.Enabled = true;
                    btnAllPrefectBatchFirst.Enabled = true;

                    if (currentPageNumber == Int32.Parse(lblAllPrefectBatchTotalPage.Text))
                    {
                        allPrefectBatchOfOnePage = allPrefectBatchData.GetRange((currentPageNumber - 1) * PREFECT_BATCH_PAGE_SIZE * PREFECT_BATCH_PER_ROW
                            , (Convert.ToInt32(totalRows) - (currentPageNumber - 1) * PREFECT_BATCH_PAGE_SIZE * PREFECT_BATCH_PER_ROW));

                        btnAllPrefectBatchNext.Enabled = false;
                        btnAllPrefectBatchLast.Enabled = false;

                    }
                    else
                    {
                        allPrefectBatchOfOnePage = allPrefectBatchData.GetRange((currentPageNumber - 1) * PREFECT_BATCH_PAGE_SIZE * PREFECT_BATCH_PER_ROW, PREFECT_BATCH_PAGE_SIZE * PREFECT_BATCH_PER_ROW);

                        btnAllPrefectBatchNext.Enabled = true;
                        btnAllPrefectBatchLast.Enabled = true;


                    }





                }
            }

            CreateDynamicTable(allPrefectBatchOfOnePage);




        }

        private int CalculateTotalPages(double totalRows)
        {
            int totalPages = 0;

            totalPages = (int)Math.Ceiling(totalRows / (PREFECT_BATCH_PAGE_SIZE * PREFECT_BATCH_PER_ROW));
            

            return totalPages;
        }

        protected void ChangePage(object sender, CommandEventArgs e)
        {

            switch (e.CommandName)
            {

                case "First":
                    currentPageNumber = 1;
                    break;

                case "Previous":
                    currentPageNumber = Int32.Parse(lblAllPrefectBatchCurrPage.Text) - 1;
                    break;

                case "Next":
                    currentPageNumber = Int32.Parse(lblAllPrefectBatchCurrPage.Text) + 1;
                    break;
                case "Last":
                    currentPageNumber = Int32.Parse(lblAllPrefectBatchTotalPage.Text);
                    break;
            }

            //Session["ol_curr_pg_no"] = ol_currentPageNumber;
            BindData();
        }


        private void CreateDynamicTable(List<PrefectBatchDTO> allPrefectBatchOfOnePage)
        {
            PlaceHolder2.Controls.Clear();

            // Fetch the number of Rows and Columns for the table 
            // using the properties


            int tblRows = allPrefectBatchOfOnePage.Count / PREFECT_BATCH_PER_ROW;
            int tblCols = 0;


            // Create a Table and set its properties 
            Table tbl = new Table();
            tbl.Width = new Unit("100%");
            tbl.BorderStyle = BorderStyle.Solid;
            // Add the table to the placeholder control
            PlaceHolder2.Controls.Add(tbl);
            // Now iterate through the table and add your controls 
            for (int i = 0; i <= tblRows; i++)
            {
                TableRow tr = new TableRow();
                tr.Width = new Unit("25%");



                if (i != tblRows && tblRows >= 0)
                {
                    tblCols = PREFECT_BATCH_PER_ROW;
                }
                else
                {
                    tblCols = allPrefectBatchOfOnePage.Count % PREFECT_BATCH_PER_ROW;
                }


                for (int j = 0; j < tblCols; j++)
                {
                    TableCell tc = new TableCell();

                    HyperLink hlAthleticYrCell = new HyperLink();
                    hlAthleticYrCell.Text = allPrefectBatchOfOnePage[i * PREFECT_BATCH_PER_ROW + j].PrefectBatchName;

                   // if (string.IsNullOrEmpty(yrWiseQueryString))
                    //{
                    hlAthleticYrCell.NavigateUrl = "viewPrefectsBatchesById.aspx?" + allPrefectBatchOfOnePage[i * PREFECT_BATCH_PER_ROW + j].PrefectBatchId;
                   // }
                    if (i % 2 == 0)
                    {
                        tc.Style.Add("background-color", "#003399");
                        tc.ForeColor = System.Drawing.Color.White;
                    }
                    else
                    {
                        tc.Style.Add("background-color", "White");
                        tc.ForeColor = System.Drawing.Color.Blue;
                    }



                    // Add the control to the TableCell
                    tc.Controls.Add(hlAthleticYrCell);
                    // Add the TableCell to the TableRow
                    tr.Cells.Add(tc);
                }

                // Add the TableRow to the Table
                tbl.Rows.Add(tr);
            }

            // This parameter helps determine in the LoadViewState event,
            // whether to recreate the dynamic controls or not

            ViewState["dynamictable"] = true;
        }
       
    }
}